package com.slz3.business.task;

import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.redis.Jedis;
import com.slz3.business.frame.redis.RK;
import com.slz3.business.frame.util.ConvertUtil;
import com.slz3.business.frame.util.ConverterUtils;
import com.slz3.business.frame.util.LocalDT;
import com.slz3.business.module.memberManagement.memJifen.domain.MemJifen;
import com.slz3.business.module.memberManagement.memJifen.domain.UserScoreRecord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;

/**
 * @FileName: MemJifenTaskTimer
 * @Description:  同步用户积分记录
 */

@Slf4j
@Component
public class MemJifenTaskTimer {



    /**
     * @Description：积分同步
     */
    private void sysncDrawTicket(){
        log.info("定时任务同步用户积分开始");
        // 循环优惠券
        String key = String.format("%s:%s", RK.SERVICE, RK.JIFEN_RECORD).concat("*");
        Set<String> keys = Jedis.db0.kes(key);
        for (String drawTickKey: keys) {
            // 查询优惠券
            List userDraTicketList = Jedis.db0.getListStartEnd(ConverterUtils.toString(drawTickKey), 0, -1);
            List<UserScoreRecord> userScoreRecordList = ConvertUtil.objToList(userDraTicketList, UserScoreRecord.class);
            for (UserScoreRecord userScoreRecord: userScoreRecordList ) {
                UUID uuid = UUID.randomUUID();
                try {
                    uuid = UUID.fromString(userScoreRecord.getId());
                }catch (Exception e){
                    log.error("UUID 格式修改");
                }
                Optional<MemJifen> memJifen = Services.memJifen.single(uuid);
                if(!memJifen.isPresent()){
                    MemJifen vo = new MemJifen();
                    vo.setUuid(uuid);// 积分ID
                    vo.setCreateTime(LocalDateTime.now());//创建时间
                    vo.setSerJifenOrderNo(userScoreRecord.getOrderId());// 订单号
                    vo.setSerJifenUserId(UUID.fromString(userScoreRecord.getUserId()));// 用户ID
                    vo.setSerJifenType(userScoreRecord.getMemJifenJifenTypeFlag());// 积分类型
                    vo.setSerJifenState(userScoreRecord.getMemJifenJifenStateFlag());//积分状态
                    vo.setSerMemo(userScoreRecord.getRemark());// 积分备注
                    vo.setSerJinfenNum(ConverterUtils.toInt(userScoreRecord.getScore(),0));// 积分值
                    long createTime = Long.parseLong(userScoreRecord.getCreateTime());
                    LocalDateTime dateTimeOfTimestamp = LocalDT.getDateTimeOfTimestamp(createTime * 1000l ) ;
                    vo.setCreateTime(dateTimeOfTimestamp);
                    Services.memJifen.save(vo);
                }else {
                    MemJifen vo = new MemJifen();
                    vo.setUuid(uuid);// 积分ID
                    vo.setCreateTime(LocalDateTime.now());//创建时间
                    vo.setSerJifenOrderNo(userScoreRecord.getOrderId());// 订单号
                    vo.setSerJifenUserId(UUID.fromString(userScoreRecord.getUserId()));// 用户ID
                    vo.setSerJifenType(userScoreRecord.getMemJifenJifenTypeFlag());// 积分类型
                    vo.setSerJifenState(userScoreRecord.getMemJifenJifenStateFlag());//积分状态
                    vo.setSerMemo(userScoreRecord.getRemark());// 积分备注
                    vo.setSerJinfenNum(ConverterUtils.toInt(userScoreRecord.getScore(),0));// 积分值
                    long createTime = Long.parseLong(userScoreRecord.getCreateTime());
                    LocalDateTime dateTimeOfTimestamp = LocalDT.getDateTimeOfTimestamp(createTime * 1000l);
                    vo.setCreateTime(dateTimeOfTimestamp);
                    Services.memJifen.modify(vo);

                }
            }
            log.info("定时任务同步用户积分结束");
        }
    }





}